package it.fflabs.dynscript.client.utils;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayBoolean;
import com.google.gwt.core.client.JsArrayInteger;
import com.google.gwt.core.client.JsArrayNumber;
import com.google.gwt.core.client.JsArrayString;

/**
 * [Francesco Foresti] : I've found this class here :<br/>
 * <a href="http://www.java2s.com/Code/Java/GWT/JsArrayUtil.htm">
 * http://www.java2s.com/Code/Java/GWT/JsArrayUtil.htm</a><br/>
 * Although the gwt-dynScript library doesn't use it,<br/>
 * I think it may be useful to the users of the library, so I<br/>
 * decided to include it here (since it was publicly
 * available anyway).<br/>
 * 
 * 
 * @author Steffen Schaefer
 * 
 */
public final class JsArrayUtil {

	private JsArrayUtil() {
	}

	/**
	 * Wraps a Java String Array to a JsArrayString for dev mode.
	 * 
	 * @param srcArray
	 *            the array to wrap
	 * @return the wrapped array
	 */
	public static JsArrayString wrapArray(String[] srcArray) {
		JsArrayString result = JavaScriptObject.createArray().cast();
		for (int i = 0; i < srcArray.length; i++) {
			result.set(i, srcArray[i]);
		}
		return result;
	}

	/**
	 * Converts a JsArray<T> to a <T>[].
	 * 
	 * @param <T>
	 * 
	 * @param jsArrayString
	 *            the array to unwrap.
	 * @param T
	 *            [] the prototype array <b>(which must be of the right length)</b>.
	 * @return the T[] parameter, filled with T objects.
	 */
	public static <T extends JavaScriptObject> T[] unwrapArray(JsArray<T> jsArray, T[] dest) {
		if(dest.length != jsArray.length()) {
			throw new ArrayIndexOutOfBoundsException("jsArray is of size " + jsArray.length() + ", while destination array is of size " + dest.length );
		}
		for (int i = 0; i < jsArray.length(); i++) {
			dest[i] = jsArray.get(i);
		}
		return dest;
	}

	/**
	 * Converts a JsArrayString to a String[].
	 * 
	 * @param jsArrayString
	 *            the array to unwrap.
	 * @return the created String[].
	 */
	public static String[] unwrapArrayString(JsArrayString jsArrayString) {
		String[] result = new String[jsArrayString.length()];
		for (int i = 0; i < jsArrayString.length(); i++) {
			result[i] = jsArrayString.get(i);
		}
		return result;
	}

	/**
	 * Wraps a Java float Array to a JsArrayNumber for dev mode.
	 * 
	 * @param srcArray
	 *            the array to wrap
	 * @return the wrapped array
	 */
	public static JsArrayNumber wrapArray(float[] srcArray) {
		JsArrayNumber result = JavaScriptObject.createArray().cast();
		for (int i = 0; i < srcArray.length; i++) {
			result.set(i, srcArray[i]);
		}
		return result;
	}

	/**
	 * Wraps a Java double Array to a JsArrayNumber for dev mode.
	 * 
	 * @param srcArray
	 *            the array to wrap
	 * @return the wrapped array
	 */
	public static JsArrayNumber wrapArray(double[] srcArray) {
		JsArrayNumber result = JavaScriptObject.createArray().cast();
		for (int i = 0; i < srcArray.length; i++) {
			result.set(i, srcArray[i]);
		}
		return result;
	}

	/**
	 * Wraps a Java int Array to a JsArrayInteger for dev mode.
	 * 
	 * @param srcArray
	 *            the array to wrap
	 * @return the wrapped array
	 */
	public static JsArrayInteger wrapArray(int[] srcArray) {
		JsArrayInteger result = JavaScriptObject.createArray().cast();
		for (int i = 0; i < srcArray.length; i++) {
			result.set(i, srcArray[i]);
		}
		return result;
	}

	/**
	 * Wraps a Java byte Array to a JsArrayInteger for dev mode.
	 * 
	 * @param srcArray
	 *            the array to wrap
	 * @return the wrapped array
	 */
	public static JsArrayInteger wrapArray(byte[] srcArray) {
		JsArrayInteger result = JavaScriptObject.createArray().cast();
		for (int i = 0; i < srcArray.length; i++) {
			result.set(i, srcArray[i]);
		}
		return result;
	}

	/**
	 * Wraps a Java boolean Array to a JsArrayBoolean for dev mode.
	 * 
	 * @param srcArray
	 *            the array to wrap
	 * @return the wrapped array
	 */
	public static JsArrayBoolean wrapArray(boolean[] srcArray) {
		JsArrayBoolean result = JavaScriptObject.createArray().cast();
		for (int i = 0; i < srcArray.length; i++) {
			result.set(i, srcArray[i]);
		}
		return result;
	}

}
